package com.gitee.smsdistribution.common_response.model;

import lombok.Getter;
import lombok.ToString;


@Getter
@ToString
public class CommonResult<T> {
    /**
     * 业务错误码
     */
    private Integer code;
    /**
     * 信息描述
     */
    private String message;
    /**
     * 信息源描述 -- 错误原因
     */
    private String description;
    /**
     * 返回参数
     */
    private T data;

    private CommonResult(ResultStatus resultStatus, T data, String description) {
        this.code = resultStatus.getCode();
        this.message = resultStatus.getMessage();
        this.description = description;
        this.data = data;
    }

    /**
     * 业务成功返回业务代码和描述信息
     */
    public static CommonResult<Void> success() {
        return new CommonResult<Void>(ResultStatus.SUCCESS, null, null);
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultStatus.SUCCESS, data, null);
    }

    /**
     * 业务成功返回业务代码,描述和返回的参数
     */
    public static <T> CommonResult<T> success(T data, String description) {
        return new CommonResult<T>(ResultStatus.SUCCESS, data, description);
    }

    /**
     * 业务成功返回业务代码,描述和返回的参数
     */
    public static <T> CommonResult<T> success(ResultStatus resultStatus, T data, String description) {
        if (resultStatus == null) {
            return success(data, description);
        }
        return new CommonResult<T>(resultStatus, data, description);
    }

    /**
     * 业务异常返回业务代码和描述信息
     */
    public static <T> CommonResult<T> failure() {
        return new CommonResult<T>(ResultStatus.INTERNAL_SERVER_ERROR, null, null);
    }

    public static <T> CommonResult<T> failure(String description) {
        return new CommonResult<T>(ResultStatus.INTERNAL_SERVER_ERROR, null, description);
    }

    /**
     * 业务异常返回业务代码,描述和返回的参数
     */
    public static <T> CommonResult<T> failure(ResultStatus resultStatus, String description) {
        return failure(resultStatus, null, description);
    }

    /**
     * 业务异常返回业务代码,描述和返回的参数
     */
    public static <T> CommonResult<T> failure(ResultStatus resultStatus, T data, String description) {
        if (resultStatus == null) {
            return new CommonResult<T>(ResultStatus.INTERNAL_SERVER_ERROR, null, description);
        }
        return new CommonResult<T>(resultStatus, data, description);
    }
}
